//
// Created by Wirano on 2020/11/9.
//

#include "Ano_Power.h"
#include "Ano_Parameter.h"
#include "Ano_Filter.h"
#include "Drv_led.h"
#include "Ano_Math.h"

float Plane_Votage = 0;
static float voltage_f = 30000;//低通滤波后的电压值
static uint8_t voltage_init_ok;

void Power_UpdateTask(uint8_t dT_ms)
{
    static int16_t voltage_s16;//ADC上采样到的电压值
    float cut_off_freq;//低通滤波器截止频率频率

    voltage_s16 = AdcValue * 8.8653f;//1.128f; ;

    if (voltage_init_ok == 0)//电压采样初始化完成
    {
        cut_off_freq = 2.0f;

        if (voltage_f > 2000 && ABS(voltage_s16 - voltage_f) < 200)
        {
            voltage_init_ok = 1;
        }
    } else
    {
        cut_off_freq = 0.02f;
    }

    LPF_1_(cut_off_freq, dT_ms * 1e-3f, voltage_s16, voltage_f);//对采样的的电压值进行低通滤波


    Plane_Votage = voltage_f * 0.001f;//换算为电压值(单位V)
//	Plane_Votage = 15;



    if (Plane_Votage < Ano_Parame.set.lowest_power_voltage)
    {
        flag.power_state = 3;//将禁止解锁
    } else
    {
        flag.power_state = 1;
    }

    if (Plane_Votage < Ano_Parame.set.warn_power_voltage)//低于警告电压
    {
        LED_STA.lowVt = 1;
    } else if (Plane_Votage > Ano_Parame.set.warn_power_voltage + 0.2f)
    {
        LED_STA.lowVt = 0;
    }

    if (Plane_Votage < Ano_Parame.set.return_home_power_voltage)
    {


    }
}

